﻿using Microsoft.Extensions.DependencyInjection;

namespace Jim.Web.Core;

public static class CacheSetup
{
    /// <summary>
    /// 缓存注册（新生命Redis组件）
    /// </summary>
    /// <param name="services"></param>
    public static void AddCache(this IServiceCollection services)
    {
        ICache cache = Cache.Default;
        var cacheOptions = App.GetOptions<CacheOptions>();
        if (cacheOptions.CacheType == CacheTypeEnum.Redis.ToString())
        {
            //services.AddStackExchangeRedisCache(options =>
            //{
            //    // 连接字符串，这里也可以读取配置文件
            //    options.Configuration = cacheOptions.Redis.Configuration;
            //    // 键名前缀
            //    options.InstanceName = cacheOptions.Redis.Prefix;
            //});
            cache = new FullRedis(new RedisOptions
            {
                // 连接字符串，这里也可以读取配置文件
                Configuration = cacheOptions.Redis.Configuration,
                // 键名前缀
                Prefix = cacheOptions.Redis.Prefix
            });
            if (cacheOptions.Redis.MaxMessageSize > 0)
                ((FullRedis)cache).MaxMessageSize = cacheOptions.Redis.MaxMessageSize;
        }
        services.AddSingleton(cache);
    }
}